<template>
  <div class="app-container">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-position="right" label-width="120px"
      style="margin-left:30px;">
      <div>
        <span style="font-size: 18px; font-weight: bold;">修改密码</span>
        <el-divider></el-divider>
      </div>
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input v-model="ruleForm.oldPassword" show-password style="width: 323.06px"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="ruleForm.newPassword" show-password style="width: 323.06px"></el-input>
        <div class="el-form-item__tip" style="color: #888;">
          密码需8-20位，且包含大小写字母和数字
        </div>
      </el-form-item>
      <el-form-item label="确认密码" prop="newPasswordCfm">
        <el-input v-model="ruleForm.newPasswordCfm" show-password style="width: 323.06px"></el-input>
      </el-form-item>
      <el-form-item>
        <div class="fixed-bottom-20">
          <el-button type="primary" @click="submitForm('ruleForm')">确认</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import { updatePwd } from '@/api/system'

export default {
  name: "EditPassword",
  data() {
    // 密码强度校验（必须包含大小写字母和数字）
    const validatePasswordStrength = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else if (value.length < 8 || value.length > 20) {
        callback(new Error('密码长度需8-20位'));
      } else if (!/(?=.*[a-z])/.test(value)) {
        callback(new Error('密码必须包含小写字母'));
      } else if (!/(?=.*[A-Z])/.test(value)) {
        callback(new Error('密码必须包含大写字母'));
      } else if (!/(?=.*\d)/.test(value)) {
        callback(new Error('密码必须包含数字'));
      } else {
        callback();
      }
    };

    // 确认密码校验
    const validateConfirmPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.newPassword) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };

    return {
      ruleForm: {
        oldPassword: '',
        newPassword: '',
        newPasswordCfm: ''
      },
      rules: {
        oldPassword: [
          { required: true, message: '请填写旧密码', trigger: 'blur' },
          { min: 8, max: 20, message: '长度在8到20个字符', trigger: 'blur' }
        ],
        newPassword: [
          { validator: validatePasswordStrength, trigger: 'blur' }
        ],
        newPasswordCfm: [
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updatePwd(this.ruleForm).then(res => {
            if (res.data?.success) {
              Message.success('密码修改成功');
              this.$router.push('/'); // 跳转首页
            } else {
              Message.error(res.data?.message || '密码修改失败');
            }
          }).catch(() => {
            Message.error('网络错误，请稍后重试');
          });
        } else {
          return false;
        }
      });
    },
    cancel() {
      this.$refs.ruleForm.resetFields(); // 重置表单
      this.$router.push('/'); // 跳转首页
    }
  }
}
</script>

<style scoped>
.app-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}
.fixed-bottom-20 {
  margin-top: 20px;
}
.el-form-item__tip {
  font-size: 12px;
  line-height: 1.5;
  margin-top: 5px;
}
</style>